//
//  LYTabBarController.m
//  LLRiseTabBarDemo
//
//  Created by LianLeven on 15/10/21.
//  Copyright © 2015年 lichangwen. All rights reserved.
//

#import "LRTabBarController.h"
#import "HeadlineController.h"
#import "PostedNewsController.h"
#import "PolicyController.h"
#import "MineController.h"
#import "HomeController.h"
#import "UIImage+LYColor.h"
#import "UIButton+LYButton.h"
#import "ArticleTypeController.h"
#import "PostedNewsController.h"
#import "ArticleTypeView.h"
#import "CompletionView.h"
#import "PostedTools.h"

#define tabSelectedColor KRedColors
#define tabNormalColor [Util colorWithHexString:@"#666666"]

@interface LRTabBarController()
@property (nonatomic, strong) ArticleTypeView *artice;
@end

@implementation LRTabBarController
- (void)viewDidLoad{
    [super viewDidLoad];
    [self setupViewControllers];
    [self configureStyle];
}
- (void)configureStyle{
    
    //去掉上部的黑色线条，
    //如果加上黑色线条的话可以这样。view自定义高度为1，宽度为tabBar的宽度 [tabbarController.tabBar addSubview:view];
    [[UITabBar appearance] setBackgroundImage:[UIImage imageWithLYColor:[UIColor clearColor]]];
    [[UITabBar appearance] setShadowImage:[UIImage new]];
    UIView *topLineView = [[UIView alloc] init];
    topLineView.frame = CGRectMake(0, 0, CGRectGetWidth(self.tabBar.bounds), 1);
    topLineView.backgroundColor = [UIColor colorWithWhite:0.966 alpha:1.000];
    [self.tabBar addSubview:topLineView];
    self.tabBar.translucent = NO;
    
    //设置中间的tabBarItem 这个可以随便自定义
    UIButton *button = [self middleTabBarItem];
    [self.tabBar addSubview:button];
    [self.tabBar bringSubviewToFront:button];
    self.tabBar.translucent = NO;
}
- (void)setupViewControllers{
    
    UINavigationController *homeVC = [self navigationControllerWithController:[HomeController class]
                                                                  tabBarTitle:@"首页"
                                                                  normalImage:[UIImage imageNamed:@"nav01"]
                                                                selectedImage:[UIImage imageNamed:@"nav01_on"]];
    UINavigationController *headlineVC = [self navigationControllerWithController:[HeadlineController class]
                                                                  tabBarTitle:@"村头条"
                                                                  normalImage:[UIImage imageNamed:@"nav02"]
                                                                selectedImage:[UIImage imageNamed:@"nav02_on"]];
    UINavigationController *policyVC = [self navigationControllerWithController:[PolicyController class]
                                                                    tabBarTitle:@"政策法规"
                                                                   normalImage:[UIImage imageNamed:@"nav04"]
                                                                 selectedImage:[UIImage imageNamed:@"nav04_on"]];
    UINavigationController *mineVC = [self navigationControllerWithController:[MineController class]
                                                                   tabBarTitle:@"我的"
                                                                normalImage:[UIImage imageNamed:@"nav05"]
                                                              selectedImage:[UIImage imageNamed:@"nav05_on"]];
    PostedNewsController *sendVC = [[PostedNewsController alloc] init];
    sendVC.tabBarItem.enabled = NO;
    sendVC.tabBarItem.title = nil;
    
    /**
     *  设置tabbar的字体颜色
     */
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:tabNormalColor, NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:tabSelectedColor, NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    
    self.viewControllers = @[homeVC,headlineVC,sendVC,policyVC,mineVC];
    
    
}
- (UINavigationController *)navigationControllerWithController:(Class)controller tabBarTitle:(NSString *)tabBarTitle normalImage:(UIImage *)normalImage selectedImage:(UIImage *)selectedImage{
    UIViewController *vc = [[controller alloc] init];;
    
    UITabBarItem *item = [[UITabBarItem alloc]
                          initWithTitle:tabBarTitle
                          image:normalImage
                          selectedImage:selectedImage];
    vc.tabBarItem = item;
    vc.title = tabBarTitle;
    item.image = [item.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    item.selectedImage = [item.selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    return nav;
}
- (UIButton *)middleTabBarItem{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake((self.tabBar.bounds.size.width-45)* 0.5, (self.tabBar.bounds.size.height - 38) * 0.5, 45, 38)];
    button.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [button setImage:[UIImage imageNamed:@"nav03"] forState:UIControlStateNormal];
    [button setTitle:@"" forState:UIControlStateNormal];
    [button setTitleColor:tabNormalColor forState:UIControlStateNormal];
    [button setTitleColor:tabSelectedColor forState:UIControlStateHighlighted];
    button.titleLabel.font = [UIFont systemFontOfSize:11];
    [button setTitlePositionWithType:ButtonTitlePostionTypeBottom];
    [button addTarget:self action:@selector(tabBarDidSelectedRiseButton) forControlEvents:UIControlEventTouchUpInside];
    return button;
}
#pragma mark - action

- (void)tabBarDidSelectedRiseButton {

    
    
    //补全信息
//    [self showCompletion];
    if ([Globle getInstance].account.account.length) {//登录
        //弹出发布选项
        [self showPostedType];
    }
    else {
//        LRAlertView *alert = [[LRAlertView alloc] initWithAlertTitle:@"" Message:@"请您先登录您的账号！" ButtonTitle:@"取消" OtherTitle:@"确定"];
//        alert.btnClickBlock = ^(NSInteger index) {
//            if (index == 1) {
//                //弹出登录
//                [self showLogin];
//            }
//        };
//        [alert showAlert];
        
        [self showLogin];
    }
}

#pragma mark - 发布文章类型
- (void)showPostedType
{
    self.artice = [[ArticleTypeView alloc] initWithShowView:self WithData:PostedTypeAry];
    [self.view addSubview:self.artice];
    WeakSelf(weakSelf);
    self.artice.nextBlock = ^(PostedType *model) {
        id obj = [LongriseRouter openURL:@"PostedViewController"];
        if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
        {
            id<LongriseRouterDelegater> delegater = obj;
            if(delegater)
            {
                //进行传值，传入统一的block以供B进行回调
                [delegater setParams:model block:^(id obj) {
                    
                    NSLog(@"回调数据：%@",obj);
                }];
            }
        }
        if (obj && [obj isKindOfClass:[UIViewController class]]) {
            UIViewController *VC = obj;
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:VC];
            [weakSelf presentViewController:nav animated:YES completion:nil];
        }
    };
}

#pragma mark - 弹出登录
- (void)showLogin
{
    [[NSNotificationCenter defaultCenter] postNotificationName:ShowLoginController object:self.selectedViewController];
}

#pragma mark - 弹出补全信息
- (void)showCompletion
{
    UIViewController *views = self.selectedViewController;
    CompletionView *completions = [[CompletionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
    completions.completionBlock = ^{
        id obj = [LongriseRouter openURL:@"CertificateViewController"];
        if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
        {
            id<LongriseRouterDelegater> delegater = obj;
            if(delegater)
            {
                //进行传值，传入统一的block以供B进行回调
                [delegater setParams:@"2" block:^(id obj) {
                    
                    NSLog(@"回调数据：%@",obj);
                }];
            }
        }
        if (obj && [obj isKindOfClass:[UIViewController class]]) {
            UIViewController *VC = obj;
            VC.hidesBottomBarWhenPushed = YES;
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:VC];
            [self presentViewController:nav animated:YES completion:nil];
        }
    };
    [views.tabBarController.view addSubview:completions];
}

@end
